package madixin.t682;

import java.util.Stack;

class Solution {
    public int calPoints(String[] ops) {
        //用栈记录数据，最后累加
        Stack<Integer> stack = new Stack<>();
        for (String op : ops) {
            switch (op) {
                case "+": {
                    Integer last = stack.pop();
                    Integer last2 = stack.peek();
                    stack.push(last);
                    stack.push(last + last2);
                    break;
                }
                case "D": {
                    stack.push(stack.peek() * 2);
                    break;
                }
                case "C": {
                    stack.pop();
                    break;
                }
                default: {
                    stack.push(Integer.parseInt(op));
                }
            }
        }
        int sum = 0;
        while (!stack.isEmpty()) {
            sum += stack.pop();
        }
        return sum;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.calPoints(new String[]{"5", "2", "C", "D", "+"}));
    }
}